echo "---------------------------------------------------"
echo "---------------------------------------------------"
echo "----------------@auther 李鹏程---------------------"
echo "----------------@auther 李鹏程---------------------"
echo "----------------@auther 李鹏程---------------------"
echo "默认audio_config.json在/ivres/baidu/asr_model/目录下；如果在其它目录，请输入路径参数"
echo "---------------------------------------------------"
echo "---------------------------------------------------"
echo "---------------------------------------------------"

adb root && adb remount && adb shell setenforce 0

account_id=$(adb shell pm list users | grep "running" | grep "GUEST" | grep -o -E '{.*?:' | sed 's/{//' | sed 's/://')
echo "account_id=${account_id}"
dump_path=None

echo "1.com.ecarx.ai.val"
echo "2.com.baidu.che.codriver"
echo "3.其它应用直接输入包名:"
read -p "请输入应用包名package:" package;
case "${package}" in
  1)
    package="com.ecarx.ai.val"
    ;;
  2)
    package="com.baidu.che.codriver"
    ;;
esac

if [[ -z $package ]]; then
  echo "包名不可以为空，exit"
  exit
else
  echo "包名=${package}"
fi

# com.ecarx.ai.val ; com.baidu.che.codriver
for package_dir in $(adb shell find /data/user/ -name "${package}");
do
  echo "package_dir=${package_dir}"
  if [[ `adb shell ls $package_dir 2> /dev/null` ]]; then
    if [[ ${package_dir} == *${account_id}* ]]; then
      dump_path="${package_dir}/files/"
      echo "dump_path=${dump_path}"
      break
    else
      echo "${package_dir}不是当前用户目录"
    fi
  else
    echo "${package_dir}目录不存在"
  fi
done
if [ ! $dump_path ]; then
  echo "dump_path为空，exit"
  exit
else
  echo "dump_path为:${dump_path}"
fi

read -p "请输入\"audio_config.json\"所在目录,为空默认设为\"/ivres/baidu/asr_model/\":" audio_config_path;
if [ -z $audio_config_path ]; then
  audio_config_path="/ivres/baidu/asr_model/"
fi

echo "\"audio_config.json\"所在目录:${audio_config_path}"

audio_config_file=$(adb shell find $audio_config_path -name "audio_config.json" -maxdepth 1)
if [ ! $audio_config_file ]; then
  echo "audio_config.json文件不在 $audio_config_path 目录,exit"
  exit
fi
echo "audio_config.json=$audio_config_file"

if [[ ! `adb shell ls ${audio_config_file}_bak 2> /dev/null` ]]; then
  adb shell cp ${audio_config_file} ${audio_config_file}_bak
fi

adb pull $audio_config_file ./audio_config.json
dump_path_line=$(cat ./audio_config.json | grep -n "dump_path" | awk -F ":" '{print $1}')
if [ ${dump_path_line} ]; then
  sed -i "" "${dump_path_line}d" audio_config.json
fi
module_name_line=$(cat ./audio_config.json | grep -n "module_name" | awk -F ":" '{print $1}')
dest_content="\"dump_path\":\"${dump_path}\","
sed -i "" "${module_name_line} a\\
${dest_content}
" audio_config.json
adb push ./audio_config.json $(dirname ${audio_config_file})
rm -f audio_config.json

adb shell mkdir -p ${dump_path}aw.so_profile
adb shell touch ${dump_path}aw.so_profile/dump_wpe
adb shell touch ${dump_path}aw.so_profile/dump_switch
adb shell touch ${dump_path}aw.so_profile/dump_switch_wakets

if [ -d ${dump_path}aud_rec ]; then
	rm -f ${dump_path}aud_rec
fi
adb shell mkdir -p ${dump_path}aud_rec

if [[ $type -eq 1 ]]; then
adb shell ln -s ${dump_path}aud_rec /data/local/ -f
adb shell ln -s ${dump_path}aw.so_profile /data/local -f
fi


#echo "1.小咖"   
#echo "2.vrDemo"
#read -p "选择测试对象:" number;
#if [[ $number -eq 1 ]]; then
#	adb shell am force-stop com.ecarx.ai.val
#else
#	adb shell am force-stop com.ecarx.ai.val.baidu
#fi
adb shell am force-stop com.ecarx.ai.val
adb shell am force-stop com.ecarx.ai.val.baidu
adb shell am force-stop com.ecarx.ai.baidurecorder
adb shell am force-stop com.baidu.che.codriver

logTempPath=log123.txt 
if [ -f ${logTempPath} ]; then
	rm -rf ${logTempPath}
fi
adb logcat -c && adb logcat  > ${logTempPath} &

echo '语料抓取结束后按任意键'
read -n 1
dir_name=aud_rec_`date +%Y%m%d_%H%M%S`
mkdir -p $dir_name
adb pull ${dump_path}/aud_rec/ ./$dir_name
adb shell killall -2 logcat
cp ${logTempPath} ./$dir_name/log123456789.txt
rm -rf ${logTempPath}
open .
echo "打开文件夹当前文件夹下的aud_rec即为语料，log123456789.txt为log"